import 'dart:async';

main(List<String> args) async {
  // var stream = Stream<int>.fromIterable([123456, 234567, 678901]);
  // var st =
  //     StreamTransformer<int, String>.fromHandlers(handleData: (int data, sink) {
  //   if (data == 678901) {
  //     sink.add("密码输入正确,正在开锁。。。");
  //   } else {
  //     sink.add("密码输入错误...");
  //   }
  // });
  // stream
  //     .transform(st)
  //     .listen((String data) => print(data), onError: (error) => print("发生错误"));

  // Duration interval = Duration(seconds: 4);
  // Stream<int> stream = Stream.periodic(interval, (data) => data); //用于创建一个周期发送事件的流，如下
  // await for (int i in stream) {
  //   print(i);
  // }

  Duration interval = Duration(seconds: 1); //如果我们想指定只发送10个事件则，用take。下面就只会打印出0-9
  Stream<int> stream = Stream.periodic(interval, (data) => data);
  stream = stream.take(4); //指定发送事件个数
  await for (int i in stream) {
    print(i);
  }
}

//https://juejin.im/post/5d3a91d1e51d457778117479#heading-4
